package com.scsj.supermarket.utils;

import android.os.Build;
import android.support.annotation.RequiresApi;
import android.util.Log;

import com.qiniu.android.http.ResponseInfo;
import com.qiniu.android.storage.UpCompletionHandler;
import com.qiniu.android.storage.UploadManager;
import com.scsj.supermarket.base.GetTokenBase;
import com.scsj.supermarket.bean.ShoperCommitInfoBean;
import com.scsj.supermarket.bean.UpLoadImgBean;
import com.scsj.supermarket.presenter.GetTokenPresenter;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.RequestBody;

/**
 * Created by Liu on 2017/7/1 0001.
 * 上传图片工具类
 */
public class ImageLoder implements GetTokenBase.IView {
    private static String load = "http://upload-z1.qiniup.com";
    private static String id;
    private GetTokenPresenter getTokenPresenter;
    private String path;
    private ShoperCommitInfoBean shoperCommitInfoBean;
    private int whichImg;

    public void startLoad(String path, ShoperCommitInfoBean shoperCommitInfoBean, int whichImg) {
        this.path = path;
        this.whichImg = whichImg;
        this.shoperCommitInfoBean = shoperCommitInfoBean;
        getTokenPresenter = new GetTokenPresenter(this);
        com.alibaba.fastjson.JSONObject jsonObject = new com.alibaba.fastjson.JSONObject();
        jsonObject.put("fileType", "2");
        jsonObject.put("suffixType", "png");
        RequestBody requestBody = RequestBody.create(okhttp3.MediaType
                .parse("application/json;charset=utf-8"), jsonObject.toString());
        getTokenPresenter.deliverData(requestBody);
    }

    //上传头像
    private void startLoadImg(final String path, final String key, final String token) {
        new Thread(new Runnable() {
            @RequiresApi(api = Build.VERSION_CODES.N)
            @Override
            public void run() {
                if (token != null) {
                    //图片名称为当前日期+随机数生成
//                    Date curDate   =   new Date(System.currentTimeMillis());//获取当前时间
//                    String   str   = curDate.toString();
                    UploadManager uploadManager = new UploadManager();
                    uploadManager.put(path, key, token,
                            new UpCompletionHandler() {
                                @Override
                                public void complete(String arg0, ResponseInfo info, JSONObject response) {
                                    if (info != null && info.statusCode == 200) {// 上传成功
                                        Log.e("QiNiuUpLoad", "上传成功！地址为：" + load + "/" + key);
                                        Log.e("QiNiuUpLoad", "response" + response.toString());

                                        //通过http://空间域名/key的形式，拿到文件的url。
                                        // upDataImg(userid,url);

                                        try {
                                            //拿到图片返回的id
                                            String data = response.getString("data");
                                            JSONObject jsonObject = new JSONObject(data);
                                            String imgId = jsonObject.getString("id");
                                            switch (whichImg) {
                                                case 0:
                                                    //店铺logo img
                                                    shoperCommitInfoBean.setShopLogoId(imgId);
                                                    break;
                                                case 1:
                                                    //店铺门头 img
                                                    shoperCommitInfoBean.setShopInfoId(imgId);
                                                    break;
                                                case 3:
                                                    //资质认证 img
                                                    shoperCommitInfoBean.setLisenceImg(imgId);
                                                    break;
                                            }
                                        } catch (JSONException e) {
                                            e.printStackTrace();
                                        }

                                    } else {
                                        Log.e("QiNiuUpLoad", "错误" + info.statusCode + ":" + info.error);
                                    }
                                }
                            }, null);
                } else {
                    Log.e("fail", "上传失败");
                }
            }
        }).start();
    }

    public static String getId() {
        return id;
    }

    @Override
    public void showSuccessMsg(String s, UpLoadImgBean upLoadImgBean) {
        //获取token成功
        String qiNiuKey = upLoadImgBean.getData().getUploadKey();
        String qiNiuToken = upLoadImgBean.getData().getUploadToken();
        //上传图片到七牛云
        startLoadImg(path, qiNiuKey, qiNiuToken);
    }

    @Override
    public void showErrorMsg(String s) {

    }
}